<template>
	<van-icon v-if="value === 1" color="#3296fa" name="good-job" @click="onLike" />
	<van-icon v-else color="#777" name="good-job-o" @click="onLike" />
</template>

<script>
	import {
		addLike,
		deleteLike
	} from '@/api/article.js'

	export default {
		props: {
			value: {
				type: Number,
				required: true
			},
			articleId: {
				type: [Number, String, Object],
				required: true
			}
		},

		methods: {
			async onLike() {
				try {
					if (this.value === 1) {
						await deleteLike(this.articleId)
						this.$emit('input', -1)
					} else {
						await addLike(this.articleId)
						this.$emit('input', 1)
					}
					this.$toast.success(this.value === 1 ? '取消点赞成功' : '点赞成功')
				} catch (e) {
					this.$toast('操作失败，请重试')
				}
			},
		},
	}
</script>

<style scoped>

</style>
